"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fillLatinNameUtil = void 0;
var updateDocItemPlainText = function (docItem) {
    var text = docItem.text, textStyleMap = docItem.textStyleMap;
    var updatedPlainText = text;
    Object.entries(textStyleMap).forEach(function (_a) {
        var key = _a[0], value = _a[1];
        updatedPlainText = updatedPlainText.replace(key, value.text);
    });
    docItem.plainText = updatedPlainText;
    return docItem;
};
var fillLatinNameUtil = function (docItemList, speciesInfoList) {
    var speciesAppearance = {};
    var genusAppearance = {};
    var textStyleMapIndex = 5000;
    var speciesOrderMap = new Map();
    speciesInfoList.forEach(function (item, index) {
        var docItemIndex = -1;
        var docItemTextIndexOf = -1;
        var findIndex = docItemList.findIndex(function (docItem) { return docItem.plainText && docItem.plainText.indexOf(item.species_c) !== -1; });
        if (findIndex != -1) {
            docItemIndex = findIndex;
            docItemTextIndexOf = docItemList[findIndex].plainText.indexOf(item.species_c);
        }
        speciesOrderMap.set(item.species_c, { info: item, docItemIndex: docItemIndex, docItemTextIndexOf: docItemTextIndexOf });
    });
    var speciesOrderList = Array.from(speciesOrderMap.values());
    speciesOrderList.sort(function (a, b) {
        if (a.docItemIndex !== b.docItemIndex) {
            return a.docItemIndex - b.docItemIndex;
        }
        else {
            return a.docItemTextIndexOf - b.docItemTextIndexOf;
        }
    });
    var speciesInfoListSorted = speciesOrderList.map(function (ele) { return ele.info; });
    docItemList.forEach(function (docItem) {
        if (!docItem.textStyleMap) {
            docItem.textStyleMap = {};
        }
        var needToUpdatePlainText = false;
        speciesInfoListSorted.forEach(function (_a) {
            var genus = _a.genus, canorical_me = _a.canorical_me, species_c = _a.species_c;
            var regex = new RegExp("".concat(species_c, "(\\s*\\(.*?\\))?(?!\\s*\\()"), "g");
            var match = null;
            while ((match = regex.exec(docItem.text)) !== null) {
                if (speciesAppearance[species_c])
                    continue;
                var isGenusFirstAppearance = genusAppearance[genus] === undefined;
                genusAppearance[genus] = false;
                speciesAppearance[species_c] = true;
                var placeholder = "$$[".concat(textStyleMapIndex, "]");
                var latinName = isGenusFirstAppearance ? canorical_me : "".concat(genus[0], ". ").concat(canorical_me.split(" ")[1]);
                if (isGenusFirstAppearance) {
                    var replacementText = "".concat(species_c, " (").concat(placeholder, ")");
                    docItem.text = docItem.text.substring(0, match.index) + replacementText + docItem.text.substring(match.index + match[0].length);
                    docItem.textStyleMap[placeholder] = {
                        text: latinName,
                        italic: isGenusFirstAppearance,
                    };
                }
                else {
                    var latinNameBefore = "".concat(genus[0], ". ");
                    var latinNameAfter = "".concat(canorical_me.split(" ")[1]);
                    textStyleMapIndex++;
                    var afterPlaceholder = "$$[".concat(textStyleMapIndex, "]");
                    docItem.textStyleMap[placeholder] = {
                        text: latinNameBefore,
                        italic: isGenusFirstAppearance
                    };
                    docItem.textStyleMap[afterPlaceholder] = {
                        text: latinNameAfter,
                        italic: true,
                    };
                    var replacementText = "".concat(species_c, " (").concat(placeholder).concat(afterPlaceholder, ")");
                    docItem.text = docItem.text.substring(0, match.index) + replacementText + docItem.text.substring(match.index + match[0].length);
                }
                needToUpdatePlainText = true;
                textStyleMapIndex++;
            }
        });
        if (needToUpdatePlainText) {
            docItem = updateDocItemPlainText(docItem);
        }
    });
    return docItemList;
};
exports.fillLatinNameUtil = fillLatinNameUtil;
